<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
<!--<form action="/login1" method="post">-->
    <form method="post">
    <h2>>>登录<<</h2>
    <input type="text" name="username" id="username"><br>
    <input type="password" name="password" id="password"><br>
    <button>登录</button>
  </form>

<script>
    let send=document.querySelector("button")
    send.addEventListener("click",function (e){
        e.preventDefault()
        let xhr = new XMLHttpRequest();
        xhr.open("post","http://localhost:8080/api/login")
        xhr.setRequestHeader("Content-Type","application/json;charset=UTF-8")
        xhr.onreadystatechange=function (){
            if(xhr.readyState===4){
                if(xhr.status===200){
                    console.log(xhr.responseText)
                    let dataJson = JSON.parse(xhr.responseText);

                    if(data.resCode===1){
                        let token=dataJson.date;
                        localStorage.setItem("token",token)
                        //登录成功，跳转到index.html
                        location.href="http://"+location.host+"/index.html"
                        // console.log(xhr.responseText)
                    }


                }else{
                    console.log("错误")
                    // console.log(xhr.status)
                    // console.log(xhr.responseText)
                }
            }
        }
        // 把用户输入的值封装成json格式
        let username=document.getElementById("username").value
        let password=document.getElementById("password").value
        let data = JSON.stringify({
            username:username,
            password:password
        })
        console.log("数据:"+data)
        // 发送数据
        xhr.send(data)
    })
</script>

</body>
</html>